package shuai.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shuai.dao.mapper.UsermeMapper;
import shuai.dao.mapper.userinfoMapper;
import shuai.model.Userme;
import shuai.model.userinfo;
import shuai.service.UserService;

import javax.annotation.Resource;
import java.util.List;
@Repository
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UsermeMapper userMapper;
    @Resource
    private userinfoMapper userinfoMapper;
    /**
     * 登录验证
     * @param loginName
     * @param password
     * @return
     * */
    public Userme loginUser(String loginName, String password) {
        Userme userme =new Userme();
        if(StringUtils.isNotEmpty(loginName)&&StringUtils.isNotEmpty(password)) {
            userme.setUsername(loginName);
            userme.setPassword(password);
        }else{
            return null;
        }
        List<Userme> usermeList =userMapper.getUsermeList(userme);
        if(usermeList.isEmpty()) {
            return null;
        }
        else{
//            String ecnryptPassowrd = usermeList.get(0).getPassword();
//            if (ecnryptPassowrd.equals(password)){
                return usermeList.get(0);
//            }
//            else{
//                return null;
//            }

        }

    }
//    @Transactional(rollbackFor=Exception.class)
    public  boolean  regiterUser(Userme userme) throws Exception {
        Userme usermel=userMapper.getUserByUserName(userme.getUsername());
        if (usermel!=null){
            return false;
        }else{
            userinfo u=new userinfo();
            u.setAddress("1");
            u.setBirth("1");
            u.setIdCard("1");
            u.setSex("1");
            u.setName("1");
            u.setPhone("1");

//            userinfoMapper.insert(u);
            if (this.inTable1(userme)){
                this.inTable2(u);
            }

            String a=null;
            System.out.println(a.toString());
            return true;
        }

    }

    public boolean inTable1(Userme userme){
        userMapper.insertSelective(userme);
        return true;
    }

    public void inTable2(userinfo u){
        userinfoMapper.insert(u);
    }
}
